package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.CategoryDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.CategoryMapper;
import com.iflytek.model.Category;
import com.iflytek.service.CategoryService;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.CategoryVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 分类菜单表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private CategoryService categoryService;

    @Resource
    private RoleService roleService;

    /**
     * @return
     */
    @Override
    public Result<List<CategoryVO>> findAll() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        List<CategoryVO> categoryVOList = CopyUtil.copyList(categoryList, CategoryVO.class);
        return Result.success(categoryVOList);
    }


    //分类分页查询
    @Override
    public Result<PageVO<CategoryVO>> getPage(PageDTO<CategoryDTO> dto) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())) {
            queryWrapper.lambda().like(Category::getName, dto.getName());
        }
        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        PageHelper.startPage(dto.getPageNum(), dto.getSize());
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        PageInfo<Category> categoryPageInfo = new PageInfo<>();
        List<CategoryVO> categoryVOS = CopyUtil.copyList(categoryList, CategoryVO.class);
        PageVO<CategoryVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(categoryPageInfo.getTotal());
        pageVO.setRecords(categoryVOS);
        return Result.success(pageVO);
    }



    /**
     * saveCategory
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveCategory(CategoryDTO dto) {
        //Category category = new Category();
        if (dto == null) {
            return Result.error("1", "内容为空");
        }
        Category category = CopyUtil.copy(dto, Category.class);
        if (StringUtils.isEmpty(category.getId())) {
            if (StringUtils.isEmpty(category)) {
                return Result.error("1", "保存失败");
            }
        }
        categoryMapper.insert(category);
        return Result.success(true);
    }

    /**
     * updateCategory
     * @param dto
     * @return
     */
    @Override
    public Result<Boolean> updateCategory(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        int count = categoryMapper.updateById(category);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("1", "更新失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public Result<CategoryVO> findById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        Category category = categoryMapper.selectById(id);
        CategoryVO categoryVO = CopyUtil.copy(category, CategoryVO.class);

        return Result.success(categoryVO);
    }


    /**
     * @param id
     * @return
     */
    @Override
    public Result<CategoryVO> deleteById(Integer id) {

        if (id == null) {
            Result.error("401", "id为空");
        }
        int i = categoryMapper.deleteById(id);
        if (i > 0) {
            Result.success();
        } else {

            Result.error("204", "错误");
        }
        return Result.success();
    }

}

